package com.boge.auth.serviceImpl;

import com.boge.auth.dto.SysDeptLevelDto;
import com.boge.auth.model.dao.mappers.SysDeptMapper;
import com.boge.auth.model.entity.SysDept;
import com.boge.auth.service.SysTreeService;
import com.boge.auth.util.LevelUtil;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class SysTreeServiceImpl implements SysTreeService {

    @Resource
    SysDeptMapper sysDeptMapper;

    private Comparator<SysDeptLevelDto> deptSeqComparator = new Comparator<SysDeptLevelDto>() {
        @Override
        public int compare(SysDeptLevelDto o1, SysDeptLevelDto o2) {
            return o1.getSeq() - o2.getSeq();
        }
    };

    private List<SysDeptLevelDto> deptLevelDtoList2Tree(List<SysDeptLevelDto> deptLevelDtoList) {
        if (CollectionUtils.isEmpty(deptLevelDtoList)) {
            return Lists.newArrayList();
        }
        //Guava-Multimap https://www.cnblogs.com/congsg2016/p/5121551.html //Multimap https://www.cnblogs.com/congsg2016/p/5121551.html
        Multimap<String, SysDeptLevelDto> deptLevelDtoMap = ArrayListMultimap.create();
        List<SysDeptLevelDto> rootList = Lists.newArrayList();

        //构建第一层级的树，并将所有dept放入map中
        for (SysDeptLevelDto dto : deptLevelDtoList) {
            deptLevelDtoMap.put(dto.getLevel(), dto);
            if (dto.getLevel().equals(LevelUtil.ROOT)) {
                rootList.add(dto);
            }
        }
        Collections.sort(rootList, deptSeqComparator);

        //递归构建树
        transformDeptTree(rootList, LevelUtil.ROOT, deptLevelDtoMap);

        return rootList;

    }

    private void transformDeptTree(List<SysDeptLevelDto> deptLevelDtoList, String level, Multimap<String, SysDeptLevelDto> deptLevelDtoMap) {
        for (SysDeptLevelDto dto : deptLevelDtoList) {
            //通过当前部门获取其下一层级的level
            String nextLevel = LevelUtil.calculateLevel(dto.getLevel(), dto.getId());
            //处理下一层级
            List<SysDeptLevelDto> tempDeptLevelDtoList = (List<SysDeptLevelDto>) deptLevelDtoMap.get(nextLevel);
            if (CollectionUtils.isNotEmpty(tempDeptLevelDtoList)) {
                //排序
                Collections.sort(tempDeptLevelDtoList, deptSeqComparator);
                //设置下一层部门
                dto.setDeptList(tempDeptLevelDtoList);
                //进入下下一层部门处理
                transformDeptTree(tempDeptLevelDtoList, nextLevel, deptLevelDtoMap);

            }
        }
    }

    //TODO:是否可不用集合，设置最高层默认值
    @Override
    public List<SysDeptLevelDto> getSysDeptTree() {
        List<SysDeptLevelDto> dtoList = Lists.newArrayList();

        List<SysDept> deptList = sysDeptMapper.getAllDepts();

        for (SysDept dept : deptList) {
            SysDeptLevelDto dto = SysDeptLevelDto.adapt(dept);
            dtoList.add(dto);
        }
        return deptLevelDtoList2Tree(dtoList);
    }

}
